package com.zhenzhang.myblog.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class JsonResult {
    private static final ObjectMapper mapper = new ObjectMapper();
    private String message = "OK";
    private int code = 200;

    public static JsonResult getInstance() {
        return new JsonResult();
    }

    public static JsonResult getInstance(int code, String message) {
        var res =  new JsonResult();
        res.setCode(code);
        res.setMessage(message);
        return res;
    }

    @Override
    public String toString() {
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public JsonResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public JsonResult setCode(int code) {
        this.code = code;
        return this;
    }
}
